package com.mm.cloud.common.mq.message;

import com.mm.cloud.common.mq.enums.ExchangeEnum;
import com.mm.cloud.common.mq.enums.RoutingKeyEnum;
import lombok.Data;

import java.io.Serializable;
import java.util.UUID;

/**
 * MQ消息
 *
 * @author mr
 * @since 2021-09-15
 */
@Data
public class RabbitMessage<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 唯一标识
     */
    private String uuid;

    /**
     * 交换机
     */
    private ExchangeEnum exchange;

    /**
     * 路由key
     */
    private RoutingKeyEnum routingKey;

    /**
     * 消息数据
     */
    private T data;

    public RabbitMessage(String uuid, ExchangeEnum exchange, RoutingKeyEnum routingKey, T data) {
        this.uuid = uuid;
        this.exchange = exchange;
        this.routingKey = routingKey;
        this.data = data;
    }

    public static <T> RabbitMessageBuilder<T> builder() {
        return new RabbitMessageBuilder<T>();
    }

    public static class RabbitMessageBuilder<T> {
        private String uuid = UUID.randomUUID().toString();
        private ExchangeEnum exchange;
        private RoutingKeyEnum routingKey;
        private T data;

        public RabbitMessageBuilder<T> data(T data) {
            this.data = data;
            return this;
        }

        public RabbitMessageBuilder<T> routingKey(RoutingKeyEnum routingKey) {
            this.routingKey = routingKey;
            return this;
        }

        public RabbitMessageBuilder<T> exchange(ExchangeEnum exchange) {
            this.exchange = exchange;
            return this;
        }

        public RabbitMessageBuilder<T> uuid(String uuid) {
            this.uuid = uuid;
            return this;
        }

        @Override
        public String toString() {
            return "RabbitMessage.RabbitMessageBuilder(uuid=" + this.uuid + ", exchange=" + this.exchange
                    + ", routingKey=" + this.routingKey + ", data=" + this.data + ")";
        }

        public RabbitMessage<T> build() {
            return new RabbitMessage<T>(this.uuid, this.exchange, this.routingKey, this.data);
        }

    }

    public RabbitMessage() {
        super();
    }

}
